#include "StdAfx.h"
#include "DataProcessorCenter.h"
#include <sstream>

CDataProcessorCenter::CDataProcessorCenter(CIOCPConnectionPool* _pConnectionPool):
m_pConnectionPool(_pConnectionPool)
{
}

CDataProcessorCenter::~CDataProcessorCenter(void)
{
}


bool CDataProcessorCenter::RecvData(CIOCPAsynConnectionSocket* _pConnection, char* _cArrBuffer, int _nLen)
{
	CRecvDataPackage* pPackage = NULL;

	std::map<CIOCPAsynConnectionSocket*,CRecvDataPackage*>::iterator  iter = m_mapConnectionPackage.find(_pConnection);
	if (iter != m_mapConnectionPackage.end())
	{
		pPackage = iter->second;
	}else{
		pPackage = new CRecvDataPackage;
		m_mapConnectionPackage[_pConnection] = pPackage;
	}
	
	int nPackage = 0;
	while((_nLen - nPackage) > 0){
		nPackage += pPackage->Package(_cArrBuffer + nPackage,_nLen - nPackage);
		if (pPackage->IsPackaged())
		{
			pPackage->Process();
			pPackage->Reset();
		}
	}
	
	return true;
}


bool CDataProcessorCenter::SendData(const std::string &_strIP, int _nPort, char* _cArrBuffer, int _nLen)
{
	CIOCPAsynConnectionSocket* pConnection = m_pConnectionPool->GetConnection(_strIP,_nPort);

	if (NULL == pConnection)
	{
		printf("Pool get connection failure\n");
		return false;
	}

	pConnection->SendData(_cArrBuffer,_nLen);
	return true;
}